﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using VoteSystem.Models;

namespace VoteSystem.Services
{
    public class PositionService
    {
        public OpResult ImportPosition(DataTable dt)
        {
            OpResult result = new OpResult();
            try
            {
                using (VoteSystemContext db = new VoteSystemContext())
                {
                    var sysConfig = db.SysConfigs.Find(1);
                    if (sysConfig.ImportPosition)
                    {
                        result.IsSuccess = false;
                        result.ErrorMessage = "已经导入职位信息，请不要重复导入！";
                        return result;
                    }
                    Position p = null;
                    foreach (DataRow row in dt.Rows)
                    {
                        p = new Position();
                        p.DeptName = row[1].ToString().Trim();
                        p.PositionName = row[2].ToString().Trim();
                        db.Positions.Add(p);
                    }
                    sysConfig.ImportPosition = true;
                    db.SaveChanges();
                    result.IsSuccess = true;
                    result.ErrorMessage = "导入成功!";
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = "导入失败,请联系管理员！"; 
            }
            return result;
        }

        public List<Position> GetAllPositions()
        {
            using (VoteSystemContext db = new VoteSystemContext())
            {
                return db.Positions.ToList(); 
            }
        }

        public OpResult ImportVoteData(DataTable dt)
        {
            OpResult result = new OpResult();
            try
            {
                using (VoteSystemContext db = new VoteSystemContext())
                {
                    var allPosition = db.Positions.ToList();

                    foreach (DataRow row in dt.Rows)
                    {
                        Employee emp = new Employee();
                        string employeeName = row[1].ToString();
                        emp.Name = employeeName;
                        string mobile = row[2].ToString();
                        emp.Mobile = mobile;
                        emp.Age = row[3] == null ? string.Empty : row[3].ToString();
                        emp.Sex = row[4] == null ? string.Empty : row[4].ToString();
                        db.Employees.Add(emp);

                        //第一志愿
                        string vote1_dept =   row[5].ToString();
                        string vote1_position = row[6].ToString();

                        //第二志愿
                        string vote2_dept = row[7].ToString();
                        string vote2_position = row[8].ToString();

                        //第三志愿
                        string vote3_dept = row[9].ToString();
                        string vote3_position = row[10].ToString();

                        EmployeeVotePosition evp = new EmployeeVotePosition();
                        evp.Employee = emp;
                        evp.Position1 = allPosition.First(p => p.DeptName == vote1_dept && p.PositionName == vote1_position);

                        evp.Position2 = allPosition.First(p => p.DeptName == vote2_dept && p.PositionName == vote2_position);
                        evp.Position3 = allPosition.First(p => p.DeptName == vote3_dept && p.PositionName == vote3_position);

                        evp.ConfigId = 1;

                        db.EmployeeVotePositions.Add(evp);
                    }

                    db.SaveChanges();

                    result.IsSuccess = true;
                    result.ErrorMessage = string.Empty;
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = "导入失败,请联系管理员!"; 
                LogHelper.Error(ex.Message);
                LogHelper.Error(ex.StackTrace);
            }
            return result;
        }
    }
}